import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
import AddEditModal from '#cpt/temp/AddEditModal'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '账期',			name: 'summary_sn', 	type:'select', loading:true, width:180, mode:'multiple' },
			{ label: '受检单位',		name: 'hosp_id', 		type:'select', loading:true, width:180, mode:'multiple' },
			{ label: '业务员',		name: 'salesman_id', 	type:'select', loading:true },
			{ label: '开票单位',		name: 'organization_name' },
			{ label: '操作时间',		names: ['start_at','end_at'], name:'time-null', type:'date-range' },
		],
		formData : [
			{ label: '账期',			name: 'summary_sn', type: 'select', 	half:true,	loading:true, required:true,
				onValue:({value,formData})=>{
					const {hosp_id} = this.formRef.getValue(['hosp_id'])
					if(value&&hosp_id)this.getBill({hosp_id,summary_sn:value})
				} ,
			},
			{ label: '受检单位',		name: 'hosp_id', 	type: 'select', 	half:true,	loading:true, nameStr:'name', required:true,
				onValue:({value,formData})=>{
					const {summary_sn} = this.formRef.getValue(['summary_sn'])
					if(value&&summary_sn)this.getBill({summary_sn,hosp_id:value})
				}
			 },
			 { label: '业务员',		name: 'salesman_id', 		type: 'select', 	half:true,
			 	required:true, nameStr:'real_name', idStr:'salesman_id'
			 },
			 { label: '账单金额',		name: 'bill_price', 	half:true, required:true },
			 { label: '新冠金额',		name: 'ncov_price', 	half:true, required:true },
			 { label: '现金金额',		name: 'cash_price', 	half:true, required:true },
		]
	}
	
	model = {}
	
	componentDidMount(){
		const { hosp_id } = $fn.query()
		if(hosp_id){ 
			this.query = { hosp_id: hosp_id}
			this.hosp_id = hosp_id
		}
		if(window.isInit){
			this.fetch()
		}
	}
	// 获取弹窗下拉框数据
	getModalSelect = async () => {
		const { formData } = this.state
		
		$fn.setModalRows(formData, ['summary_sn', 'hosp_id', 'salesman_id'], ()=>{
			$cache.then( async f => {
				// 账期
				$fn.setRow(formData, 'summary_sn', await f.getCache({ api:'bill-summary/select' } ) ) 
				// 医院名称
				$fn.setRow(formData, 'hosp_id', await f.getCache({ api:'hospital' } ) )
				// 业务员
				$fn.setRow(formData, 'salesman_id', await f.getCache({ api:'salesman' } ) )
				// 设置医院名称
				this.searchFormRef.setValue({ hosp_id: this.hosp_id })
				this.setState({ formData })
			})
		})
	}
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 账期
			$fn.setRow(forms, 'summary_sn', await f.getCache({ api:'bill-summary/select' } ) ) 
			// 业务员
			$fn.setRow(forms, 'salesman_id', await f.getCache({ api:'salesman' } ) )
			// 医院名称
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'hospital' } ) )
			// 设置医院名称
			this.searchFormRef.setValue({ hosp_id: this.hosp_id })
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'fi-bill-book/index', { isIndex:true })
	
	// 表单
	cols = [
		{ title: '账期', 		dataIndex: 'summary_sn', 		width:100 },
		{ title: '单位编码', 	dataIndex: 'hosp_code', 		width:100 },
		{ title: '受检单位', 	dataIndex: 'hosp_name', 		width:200 },
		{ title: '业务员编码', 	dataIndex: 'salesman_code', 	width:100 },
		{ title: '业务员', 		dataIndex: 'salesman_user', 	...$fn.table.name },
		{ title: '开票单位', 	dataIndex: 'organization_name', width:200 },
		{ title: '账单金额', 	dataIndex: 'bill_price', 		...$fn.table.num },
		{ title: '新冠金额', 	dataIndex: 'ncov_price', 		...$fn.table.num },
		{ title: '现金金额', 	dataIndex: 'cash_price', 			...$fn.table.num },
		{ title: '操作时间', 	dataIndex: 'created_at', 			...$fn.table.time },
		TableControls.controls.call(this, { width: 80 })
	]
	
	// 表格操作按钮
	table_controls = (text,{ uuid, summary_sn,hosp_id,salesman_id }) => {
		return [
			TableControls.edit.call(this, 'fi-standing-book/info', {uuid,summary_sn,hosp_id,salesman_id},{
				select: this.getModalSelect,
				click:()=>{
					const { formData } = this.state
					$http.submit(null,'fi-standing-book/getHospSummaryPrice',{param:{summary_sn,hosp_id, type:1}}).then(data=>{
						const salemanData = data.salesman
						$fn.setRow(formData, 'salesman_id', salemanData )
						this.setState({ formData })
					})
				}
			}),
		]
	}
	
	// 操作按钮
	controls = () => {
		return [
			// 添加
			Controls.add.call(this,{ click:this.getModalSelect}),
			// 导入
			Controls.imports.call(this,'fi-bill-book/import'),
			this.hosp_id && Controls.back.call(this)	// 返回
		]
	}
	
	getBill = param => {
		const { formData } = this.state
		$http.submit(null,'fi-standing-book/getHospSummaryPrice',{param:{...param, type:1}}).then(data=>{
			const salemanData = data.salesman
			$fn.setRow(formData, 'salesman_id', salemanData )
			this.setState({ formData })
			this.formRef.setValue({
				estimate_price:data.summary_sn.estimate_price,
				organization_name:data.hosp.hosp_name,
			})
		})
	}
	
	render(){
		return (
			<>
				<Page title='账单金额' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this, {
						onForm:()=>{
							this.getSelect()
						}
					}) }
					{/* 表格 */}
					{ Table.call(this,{isInit:window.isInit, isIndex:true}) }
					{/* 添加、编辑 */}
					{ AddEditModal.call(this,{ labelWidth:'s9', width:800, api: 'fi-bill-book/add',editApi:'fi-bill-book/add'}) }
				</Page>
			</>
		)
	}
}

export default Index